<%-- 
    Document   : addnew
    Created on : Sep 15, 2011, 7:55:25 PM
    Author     : bi
--%>

<%@page import="BussinessLayer.MembersFactory"%>
<%@page import="DTO.Member.Members"%>
<%@page import="BussinessLayer.ReportsFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <c:set var="chkCurMember" value="${sessionScope.USER}"/>
        <%--
        <c:if test="${empty chkCurMember}">
            <c:redirect url="index.jsp"/>
        </c:if>
        --%>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/main.css" />
        <link rel="stylesheet" type="text/css" href="css/addnew.css" />
        <title>Add New</title>

        <script type="text/javascript">
            //var reload = self.setInterval(document.attributes.submit(), 3000);
            //var auto = self.setInterval("autoGenXml()", 50 * 1000);

            function autoGenXml(){
            <%
                        String realPath = getServletContext().getRealPath("/") + "WEB-INF/";
                        Members mem = new Members();
                        try {
                            mem = (Members) session.getAttribute("USER");
                            MembersFactory memFac = new MembersFactory();
                            memFac.genXmlFromDB(mem, realPath);
                        } catch (Exception ex) {
                        }
            %>
            }

            var fullnameValidate = function() {
                if(!document.getElementsByName("txtFullname")[0].value) {
                    document.getElementById("lblFullname").innerHTML = "Fullname is required!";
                    document.getElementById("btnAdd").disabled = true;
                    return false;
                }
                document.getElementById("lblFullname").innerHTML = "";
                document.getElementById("btnAdd").disabled = false;
                return true;
            }

            var phoneValidate = function() {
                var phone = document.getElementsByName("txtPhone")[0].value;
                if(phone && !(new RegExp("^[0-9]{6,11}$")).test(phone)) {
                    document.getElementById("lblPhone").innerHTML = "Phone is invalid!";
                    document.getElementById("btnAdd").disabled = true;
                    return false;
                }
                document.getElementById("lblPhone").innerHTML = "";
                document.getElementById("btnAdd").disabled = false;
                return true;
            }

            var emailValidate = function() {
                var email = document.getElementsByName("txtEmail")[0].value;
                var reg = new RegExp("^[a-zA-Z_0-9]+([-+.'][a-zA-Z_0-9]+)*@[a-zA-Z_0-9]+([-.][a-zA-Z_0-9]+)*\.[a-zA-Z_0-9]+([-.][a-zA-Z_0-9]+)*$");
                if(email && !reg.test(email)) {
                    document.getElementById("lblEmail").innerHTML = "Email is invalid!";
                    document.getElementById("btnAdd").disabled = true;
                    return false;
                }
                document.getElementById("lblEmail").innerHTML = "";
                document.getElementById("btnAdd").disabled = false;
                return true;
            }            
        </script>
    </head>
    <body class="default" onload="self.setInterval(autoGenXml(), 50 * 1000); ">
        <div class="logo">MULTI LEVEL MANAGEMENT</div><br/>
        <form action="Controler" method="POST">
            <table class="buttongroups">
                <tr>
                    <td>
                        <button class="good" type="submit" name="btnAction" value="view"
                                onclick="location.href='viewtree.jsp'">View Tree</button>
                    </td>
                    <td>
                        <button class="quite" type="submit" name="btnAction" value="commission"
                                onclick="location.href='commission.jsp'" >Commission</button>
                    </td>
                    <td>
                        <button class="average" type="submit" name="btnAction" value="add"
                                onclick="location.href='addnew.jsp'">Add New</button>
                    </td>
                    <td>
                        <button class="bad" type="submit" name="btnAction" value="logout">Log Out</button>
                    </td>
                </tr>
            </table>
            <!--        </form>-->
            <div class="workarea">
            <% ReportsFactory reportFac = new ReportsFactory();
                        Members member = new Members();
                        member = (Members) session.getAttribute("USER");
                        if (!reportFac.checkFullChild(member)) {%>
                <!--            <form action="Controler" method="POST">-->
                <div class="regarea">
                    Required Information:<br/>
                    <table class="regtable">
                        <td class="label">
                            <!--                                Username:
                                                    <tr>
                                                        </td>
                                                        <td class="receive">
                                                            <input class="textbox" type="text" name="regUsername"/>
                                                        </td>
                                                    </tr>
                            -->
                        <tr>
                            <td class="label">
                                Full Name:
                            </td>
                            <td class="receive">
                                <input class="textbox" type="text" name="txtFullname" onblur="fullnameValidate()" onkeyup="fullnameValidate()"/>
                                <label id="lblFullname" style="color: aqua;"/>
                            </td>
                        </tr>
                        <!--                <tr>
                                                <td class="label">
                                                    Password:
                                                </td>
                                                <td class="receive">
                                                    <input class="textbox" type="password" name="txtPassword"/>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="label">
                                                    Retype Password:
                                                </td>
                                                <td class="receive">
                                                    <input class="textbox" type="password" name="regPasswordRetype" onblur="validate()"/>
                                                    <label id="lblRePass" style="color: red"/>
                                                </td>
                                            </tr>-->
                    </table>
                </div>
                <div class="regarea">
                    Addition Information:<br/>
                    <table class="regtable">                        
                        <tr>
                            <td class="label">
                                Phone:
                            </td>
                            <td class="receive">
                                <input class="textbox" type="text" name="txtPhone" maxlength="11" onblur="phoneValidate()"/>
                                <label id="lblPhone" style="color: aqua;"/>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                Email:
                            </td>
                            <td class="receive">
                                <input class="textbox" type="text" name="txtEmail" onblur="emailValidate()"/>
                                <label id="lblEmail" style="color: aqua;"/>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                Address:
                            </td>
                            <td class="receive">
                                <input class="textbox" type="text" name="txtAddress"/>
                            </td>
                        </tr>
                        <tr>
                            <td class="label">
                                Note:
                            </td>
                            <td class="receive">
                                <textarea cols="50" rows=""  class="textbox" name="txtNote"></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td class="receive">
                                <button class="average" type="submit" id="btnAdd" name="btnAction" 
                                        value="addNew" onclick="fullnameValidate();" >
                                    <img src="img/tick.png" alt="add new member"/>
                                    Add New
                                </button>
                            </td>
                            <td class="receive">
                                <button class="average" type="reset">
                                    <img src="img/cross.png" alt=""/>
                                    Reset
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            <%} else {%>
            <span class="cantadd"><font color="maroon">You've already added two members. Cannot add more!!!</font></span>
            <%}%>
            </div>
        </form>

    </body>
</html>
